import logging
import os

import uvicorn
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles

from api.router import api_router
from utils.middleware import register_middleware
from utils.template.template import register_template
from web.router import web_router
from utils.paths import RootPath, StaticsPath, UploadPath, TempPath, LogPath
from utils.exception import fastapi_exception_handler
from utils.logger import Logger

# 创建FastAPI实例
app = FastAPI()

# 密钥
secret_key = "f4ec60b46a594f4c92591ea8be030609"

# 注册中间件
register_middleware(app, secret_key)

# 注册全局异常处理
fastapi_exception_handler(app)

# 注册路由
app.include_router(api_router())  # 注册api路由
app.include_router(web_router())  # 注册web路由

# 配置静态文件路由和路径
app.mount("/static", StaticFiles(directory=StaticsPath), name="static")
app.mount("/upload", StaticFiles(directory=UploadPath), name="upload")
app.mount("/temp", StaticFiles(directory=TempPath), name="temp")

# 注册模板
register_template()

Logger.info(f"当前运行目录: {RootPath}")


if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=2345)
